﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DomainInfoLib
{
    public class AverageRequester:IRequester
    {

        APIPool _APIPool;


        public RegisterInfo GetResponseFromAPI(string DomainName, APIPool the_APIPool)
        {
            _APIPool = the_APIPool;

            return GetResponseFromAPI(DomainName);
        }

        public RegisterInfo GetResponseFromAPI(string DomainName)
        {
            try
            {
                for (int i = 0; i < _APIPool.Count; i++)
                {
                    IWebAPI api = GetAApi();
                    RegisterInfo info = api.GetResponseByWebAPI(DomainName);

                    if (info != null)
                    {
                        return info;
                    }
                    //else
                    //{
                    //    return new RegisterInfo() { Avaliable = null, DomainName = DomainName, Response = api.ToString() };
                    //}
                }
                return new RegisterInfo() { Avaliable = null, DomainName = DomainName, Response = "都没有响应" };

            }
            catch (Exception e)
            {

                return new RegisterInfo() { Avaliable = null, DomainName = DomainName, Response = e.Message };
            }

        }

        int index = 0;

        private IWebAPI GetAApi()
        {
            IWebAPI api = _APIPool[index];
            index++;
            if (index == _APIPool.Count)
            {
                index = 0;
            }
            return api;
        }
    }
}
